package cn.edu.ecjtu.android_tutorials;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SecondPage extends AppCompatActivity {
    EditText name;
    EditText tel;
    String pos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second_page);
        final Intent intent = getIntent();
        pos = intent.getStringExtra("pos");

        name = (EditText) findViewById(R.id.name);
        tel = (EditText) findViewById(R.id.phone);
        if (pos != "-1") {
            name.setText(intent.getStringExtra("name"));
            tel.setText(intent.getStringExtra("phone"));
        }
        Button button1 = (Button) findViewById(R.id.btn_confirm);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent = new Intent(SecondPage.this,MainActivity.class);
                Log.e("pos", pos);
                intent.putExtra("name", name.getText().toString());
                intent.putExtra("phone", tel.getText().toString());
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        Button button2 = (Button) findViewById(R.id.btn_cancel);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("pos",pos);
                if (Integer.parseInt(pos) != -1) {

//                    intent.putExtra("name", name.getText());
//                    intent.putExtra("phone", tel.getText());
                    intent.setClass(SecondPage.this, Editpage.class);
                    startActivity(intent);
                    finish();
                } else {

                    finish();
                }

            }
        });
    }
}